/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_blob_accessor.c                                */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/14 by songbb                               */
/*******************************************************************/

#include <stdio.h>

#include "frcomm.h"

#include "conntrack.h"
#include "frct_comm.h"

#include "../../ora_core.h"
#include "../../comm/ora_util.h"
#include "../ora_mar_engine.h"
#include "ora_blob_accessor.h"
#include "../ora_ttc_proto.h"

/*******************************************************************************
*  FUNC     :  解析 blob 类型数据
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*           :  *p_st_metadata                       (meta信息)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_blob_accessor_unmarshal_one_row(Conntrack *p_st_conn
                                        , L7session_ora *p_st_sess
                                        , L7_ora_packet *p_st_ora_packet
                                        , Sql_metadata_info *p_st_metadata)
{
    long long ll_tmp = 0LL;
    long long ll_prefetchedLength = 0LL;
    short s_tmp = 0;
    int i_ret = 0;
    int i_isPrefetched;
    int i_lob_prefetch_size_for_this_column = 0;

    if (p_st_metadata->i_isuseless == FR_YES || p_st_metadata->i_isnullbydescribe == FR_YES)
        goto success;

//    if (p_st_sess->i_use_oci == FR_NO)
//    {
        ORA_PRINT_INFO("blob_accessor up\n");
        /**
         * 280 ./apm -n -t1 -d /mnt/hgfs/intellij14/apm/global/pcap/data_dev/oracle/jdbc_oci8/12.1.0.2z/11.2.0.2.0_12.1.0.2_jdbc_oci8_gb2312_all.pcap
         *      is use oci? [true]
         *      is use special field? [true]
         *      tns version send? [314]
         *      tns version? [314]
         *      ttc version? [6]
         *      i_ptr_len? [8]
         *
         * 43 ./apm -n -t1 -d /mnt/hgfs/intellij14/apm/global/pcap/data_dev/oracle/12c/jdbc/jdbc_12.1.0.1.0_12.1.0.1.0_insert_blob_into_mydatatype.pcap
         *      is use oci? [false]
         *      is use special field? [false]
         *      tns version send? [315]
         *      tns version? [315]
         *      ttc version? [7]
         *      i_ptr_len? [unset]
         *
         * 48 ./apm -n -t1 -d /mnt/hgfs/intellij14/apm/global/pcap/data_dev/oracle/12c/jdbc/jdbc_12.1.0.1.0_12.1.0.1.0_insert_blob_into_mydatatype.pcap
         *      0156 0205c5 021f7c fe0205c5ffd8ffe000104a46494600010100000100010000ffdb0043000201010101010201010102020202020403020202020504040304060506060605060606070908060709070606080b08090a0a0a0a0a06080b0c0b0a0c090a0a0affdb004301020202020202050303050a0706070a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0affc00011080030003003012200021101031101ffc4001f0000010501010101010100000000000000000102030405060708090a0bffc400b5100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc4001f0100030101010101010101010000000000000102030405060708090a0bffc400b51100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00fd30a9f4cd36ff0058d420d334cb579a7b894245147d58fa5415dcfece7124bf146d5645076db4ecb9ec767ff5cd7d3d7a8e95194d743cfd5b49773b4f087ecd3a259d9aea7e34b992e6503749671498893d891cb7e82b91fda2b5ff00803f0fbc192ea9adfc47f09f82adf4cd4a3b2b99f5fd421b1824b9950b45034f3b28576504a8c90d83d71c7d10211e534650104700f715f981ff00073ffec75a07c7bfd85eebe22dac79f10f83b53b6b9d06d50e12f1de4f29e09012011e5c923231e51c60643907e75e32aa6aa4a474fb0a7cb6b6e7b7697aaaeac6f3c8b7745b5bd92d4b4831e63c670e40f40d95c9ea549e956b8edfad7e617fc1103f6edf887f143e11d8fc23fda02e352d4edfc132a596877d6f6c2206151fba374c4992f645f9b2c0e570a591f86afd3b82686e605b8b695648dd4323a364303d0d7b585c5c3131ba7b6e7338ce0ecc757a07ecd896c7e230967277c767234383df8073f8135e7e4e066b5bc15e227f09789ec7c448370b5b80d22f764e8c3f2fd6b6c541d4a12892e7c8d3f347d69183ce4fd2bc13f6faf81dac7c78f83d3785341d2cdedec37905c5b5a3cc122665915bcc7ddc1d8a1b683d0b035ee9a55e2ea1651dec4ca639503c6ca782a4641fcaa1f12ea56ba468577aade9021b681a597dc28c9ff0af96953528f2b47a57b6a8fcccf86fff0004a8fd983f65af8b1e2af1e783b4fd4bfb7fc5b696475f65d4185adbdc47fbd66b487188096203632a42e028048af7482d6d6c6d92caca058a28942a468a02a8f40074ab9ac5e4fab6b171a95dff00adb99da57e73b4b1ce3f0ce3f0aab5f4f87a31a349452e879fcd29eac046eff77a77ad2f0af86aefc57e21b4d02d837fa54c237754ddb149c16fc067af1d2bd5bc17fb315bc31a5df8d35433363fe3d2c98aa7d19cfcc7f0c57a7e81e0ef0ef86ed16d341d220b441d442982df53d4fe26b8eae654f58c351fd5e7515dedd8b7a3d8a69ba7c36311ca4512a267ae0003fa543e29d1a3d7f40bcd1647651796cd097500eddc319e6b4000a30052300c306bc857dceeb5958f8f754d2eff0040d467d2b50b768e6b79591d65183c1c679f5aac7af35f59f8a3c09e17f16db9b7d7b458ae41180ce30cbf461c8fc2bcb7c5ff00b303e5eefc1ba8b281ff002e97c73ff7cb8fea3f1af6a8e614e4b967a1c6e8ce2ddb63ffd9005600540001010c00800001000000010000010fa4a100016a6100016a60000700070000e000104a4649460001010000000300000000006c45ee000000000000000000000000000000000000000000016a60024000d40000
         *      is use oci? [false]
         *      is use special field? [false]
         *      tns version send? [315]
         *      tns version? [315]
         *      ttc version? [7]
         *      i_ptr_len? [unset]
         *
         *
         */
        if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_tmp) != FR_SUCC)
            goto error;

        if (ll_tmp == 0LL)
            goto success;

        if (ora_mar_peekUB1(p_st_ora_packet, &s_tmp) != FR_SUCC)
            goto error;
        if (ora_mar_escapeSequenceNull(s_tmp, &i_ret) != FR_SUCC)
            goto error;
        if ((i_ret != 1 && s_tmp == 0xfe) || (ll_tmp == s_tmp))
            i_isPrefetched = FR_NO;
        else
            i_isPrefetched = FR_YES;

        ORA_PRINT_DBG("set BLOB i_isPrefetched = %s\n", i_isPrefetched == FR_YES ? "FR_YES" : "FR_NO");

        if (i_isPrefetched == FR_YES)
        {
            if (ora_mar_unmarshalSB8(p_st_sess, p_st_ora_packet, &ll_prefetchedLength) != FR_SUCC) // prefetchedLength
                goto error;
            if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_tmp) != FR_SUCC) // prefetchedChunkSize
                goto error;
            if (p_st_sess->i_useLobPrefetch != APM_ORA_FLAG_UNSET)
            {
                if (p_st_sess->i_useLobPrefetch == FR_YES && ora_get_jdbc_type(p_st_metadata->i_data_type) == ORA_JDBCTYPE_BLOB)
                    i_lob_prefetch_size_for_this_column = p_st_metadata->i_ora_oacmxl;
                else
                    i_lob_prefetch_size_for_this_column = -1;
                if (i_lob_prefetch_size_for_this_column > 0)
                {
                    if (ora_mar_skipCLR(p_st_sess, p_st_ora_packet) != FR_SUCC) // PrefetchedData
                        goto error;
                }
            }
            else // XXX: 假设存在PrefetchedData
            {
                if (ora_mar_skipCLR(p_st_sess, p_st_ora_packet) != FR_SUCC) // PrefetchedData
                    goto error;
            }
        }

        if (ora_mar_skipCLR(p_st_sess, p_st_ora_packet) != FR_SUCC)
            goto error;
//    }
//    else
//    {
//        ORA_PRINT_INFO("blob_accessor down\n");
//        /**
//            * oci 314 5
//            * ./apm -n -w 5 -d ../oracle/pcap/sqlplus_oci_all_v2/11.1.0.6_z_windows_gb2312/sqlplus_gb2312_192.168.18.167_11.1.0.6_z_windows_2_192.168.18.168_11.2.0.2_a.pcap
//            *
//            */
//        ORA_PRINT_PACKET_CURRENT("a", p_st_ora_packet);
//        if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_tmp) != FR_SUCC) // m
//            goto error;
//        if (ll_tmp == 0LL)
//            goto success;
//        if (ora_mar_skipCLR(p_st_sess, p_st_ora_packet) != FR_SUCC)
//            goto error;
//    }

success:
    return FR_SUCC;
error:
    return FR_FAIL;
}
